<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>泳池信息</title>
    <!-- 引入element-ui样式 -->
    <link rel="stylesheet" href="../../../plugins/element-ui/index.css">


    <!--    上传图片样式-->
    <style>
        .avatar-uploader .el-upload {
            border: 1px dashed #d9d9d9;
            border-radius: 6px;
            cursor: pointer;
            position: relative;
            overflow: hidden;
        }
        .avatar-uploader .el-upload:hover {
            border-color: #409EFF;
        }
        .avatar-uploader-icon {
            font-size: 28px;
            color: #8c939d;
            width: 178px;
            height: 178px;
            line-height: 178px;
            text-align: center;
        }
        .avatar {
            width: 178px;
            height: 178px;
            display: block;
        }
    </style>
</head>
<body>

<div id="app">
    <el-container style="height: 800px; border: 1px solid #eee">
        <el-container>
            <div style="width: 100%;position: relative">

                <div style=";margin-bottom: 80px;">
                    <el-input
                            placeholder="请输入泳池名"
                            clearable
                            style="width: 20%; "
                            prefix-icon="el-icon-search"
                            v-model="Query.name"
                    >
                    </el-input>
                    <el-button @click="getAll()" class="dalfBut">查询</el-button>

                    <el-button type="warning" style="float: right" @click="Up_Add_dialogShow(null)">新增泳池</el-button>
                </div>

                <template>
                    <el-table :data="dataList" border style="width: 100%">
                        <el-table-column fixed prop="name" label="泳池名" width="150" align="center"></el-table-column>
                        <el-table-column prop="price1" label="个人票价" width="120" align="center"></el-table-column>
                        <el-table-column prop="price2" label="团购票价" width="120" align="center"></el-table-column>
                        <el-table-column prop="type" label="类型" width="120" align="center"></el-table-column>
                        <el-table-column prop="openTime" label="开放时间" width="300" align="center"></el-table-column>
                        <el-table-column prop="image" label="图片" width="120" align="center">
                            <template slot-scope="{ row }">
                                <img style="width: auto; height: 2px; border:none;cursor: pointer;"
                                     :preview-src-list=" `/files/downloadFile?name=${row.image}` "
                                     :src="getImage(row.image)"
                                />
                                <div slot="error" class="image-slot">
                                    <img :src="imageUrl1"  style="width: auto; height: 40px; border:none;" >
                                </div>
                            </template>
                        </el-table-column>
                        <el-table-column prop="address" label="地址" width="120" align="center"></el-table-column>
                        <el-table-column prop="warning" label="警示内容" width="120" align="center"></el-table-column>
                        <el-table-column prop="details" label="描述" width="120" align="center"></el-table-column>


                        <el-table-column fixed="right" label="操作图片组" width="200" align="center">
                            <template slot-scope="scope">
                                <el-button type="primary"   @click="image_dialogShow(scope.row)" type="text" size="small">编辑</el-button>
                            </template>
                        </el-table-column>

                        <el-table-column fixed="right" label="操作" width="200" align="center">
                            <template slot-scope="scope">
                                <el-button type="primary"   @click="Up_Add_dialogShow(scope.row)" type="text" size="small">编辑</el-button>
                                <el-button type="danger"  @click="DeleteNatatorium(scope.row)" type="text" size="small">删除</el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                </template>

                <!--分页组件-->
                <el-pagination
                        class="pagination"
                        @current-change="handleCurrentChange"
                        :current-page="pagination.currentPage"
                        :page-size="pagination.pageSize"
                        layout="total, prev, pager, next, jumper"
                        :total="pagination.total">
                </el-pagination>


                <!--图片组弹框-->
                <el-dialog :title="title" style="height: 650px"  :visible.sync="dialog_image">
                    <el-upload
                            class="avatar-uploader"
                            action="/files/uploadFile"

                            list-type="picture-card"
                            :file-list="fileList"
                            limit=4

                            :on-success="handleAvatarSuccess1"
                            :before-upload="beforeUpload"
                            ref="upload"
                    >
                        <i slot="default" class="el-icon-plus"></i>
                        <div slot="file" slot-scope="{file}">
                            <img
                                    class="el-upload-list__item-thumbnail"
                                    :src="file.url" alt=""
                            >
                            <span class="el-upload-list__item-actions">
                                <!--放大-->
                                <span class="el-upload-list__item-preview"
                                        @click="handlePictureCardPreview(file)">
                                  <i class="el-icon-zoom-in"></i>
                                </span>

                                <!--删除-->
                                <span v-if="!disabled"
                                        class="el-upload-list__item-delete"
                                        @click="handleRemove(file)">
                                  <i class="el-icon-delete"></i>
                                </span>
                          </span>
                        </div>
                    </el-upload>
                    <!--图片组图片放大-->
                    <el-dialog :visible.sync="dialogVisible">
                        <img width="100%" :src="dialogImageUrl" alt="">
                    </el-dialog>
                    <el-button type="primary" @click="UpdateNatatorium">确定</el-button>
                    <el-button @click="center">取消</el-button>
                </el-dialog>

                <!--2.修改 泳池弹框-->
                <el-dialog :title="title" style="height: 650px"  :visible.sync="dialog_update">
                    <el-form :model="formData_update" :rules="rules" ref="formData_update" label-width="100px" class="demo-formData_update">
                        <el-form-item  label="泳池名：" prop="name">
                            <el-input v-model="formData_update.name"></el-input>
                        </el-form-item>

                        <el-form-item  label="个人票价：" prop="price1">
                            <template>
                                <el-input-number v-model="formData_update.price1"  :min="1" :max="10000" label="描述文字"></el-input-number>
                            </template>
                        </el-form-item>

                        <el-form-item  label="团购票价：" prop="price2">
                            <template>
                                <el-input-number v-model="formData_update.price2"  :min="1" :max="10000" label="描述文字"></el-input-number>
                            </template>
                        </el-form-item>

                        <el-form-item  label="泳池类型：" prop="type">
<!--                            <el-input v-model="formData_update.type"></el-input>-->
                            <el-select v-model="formData_update.type" placeholder="请选择">
                                <el-option
                                        v-for="item in options"
                                        :key="item.typeName"
                                        :label="item.typeName"
                                        :value="item.typeName">
                                </el-option>
                            </el-select>
                        </el-form-item>

                        <el-form-item  label="开放时间：" prop="openTime">
                            <el-input v-model="formData_update.openTime"></el-input>
<!--                            <template>-->
<!--                                <el-time-picker-->
<!--                                        is-range-->
<!--                                        v-model="value1"-->
<!--                                        range-separator="至"-->
<!--                                        start-placeholder="开始时间"-->
<!--                                        end-placeholder="结束时间"-->
<!--                                        placeholder="选择时间范围">-->
<!--                                </el-time-picker>-->

<!--                            </template>-->
                        </el-form-item>

                        <el-form-item  label="地址：" prop="address">
                            <el-input v-model="formData_update.address"></el-input>
                        </el-form-item>

                        <el-form-item  label="警示信息：" prop="warning">
                            <el-input v-model="formData_update.warning"></el-input>
                        </el-form-item>

                        <el-form-item  label="描述详情：" prop="details">
                            <el-input v-model="formData_update.details"></el-input>
                        </el-form-item>

                        <el-form-item  label="图片：" prop="image">

                            <el-upload class="avatar-uploader"
                                       action="/files/uploadFile"
                                       :show-file-list="false"
                                       :on-success="handleAvatarSuccess"
                                       :before-upload="beforeUpload"
                                       ref="upload">
                                <img v-if="imageUrl" :src="imageUrl" class="avatar">
                                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                            </el-upload>

                        </el-form-item>

                        <el-form-item>
                            <el-button type="primary" @click="UpdateNatatorium">确定修改</el-button>
                            <el-button @click="center">取消</el-button>
                        </el-form-item>
                    </el-form>
                </el-dialog>

                <!--3.新增 泳池弹框-->
                <el-dialog :title="title" style="height: 650px"  :visible.sync="dialog_add">
                    <el-form :model="formData_add" :rules="rules" ref="formData_add" label-width="100px" class="demo-formData_add">
                        <el-form-item  label="泳池名：" prop="name">
                            <el-input v-model="formData_add.name"></el-input>
                        </el-form-item>

                        <el-form-item  label="个人票价：" prop="price1">
                            <template>
                                <el-input-number v-model="formData_add.price1"  :min="1" :max="10000" label="描述文字"></el-input-number>
                            </template>
                        </el-form-item>

                        <el-form-item  label="团购票价：" prop="price2">
                            <template>
                                <el-input-number v-model="formData_add.price2"  :min="1" :max="10000" label="描述文字"></el-input-number>
                            </template>
                        </el-form-item>

                        <el-form-item  label="泳池类型：" prop="type">
                            <el-select v-model="formData_add.type" placeholder="请选择">
                                <el-option
                                        v-for="item in options"
                                        :key="item.typeName"
                                        :label="item.typeName"
                                        :value="item.typeName">
                                </el-option>
                            </el-select>
                        </el-form-item>

                        <el-form-item  label="开放时间：" prop="openTime">
                            <el-input v-model="formData_add.openTime"></el-input>
                        </el-form-item>

                        <el-form-item  label="地址：" prop="address">
                            <el-input v-model="formData_add.address"></el-input>
                        </el-form-item>

                        <el-form-item  label="警示信息：" prop="warning">
                            <el-input v-model="formData_add.warning"></el-input>
                        </el-form-item>

                        <el-form-item  label="描述详情：" prop="details">
                            <el-input v-model="formData_add.details"></el-input>
                        </el-form-item>

                        <el-form-item  label="图片：" prop="image">
                            <el-upload class="avatar-uploader"
                                       action="/files/uploadFile"
                                       :show-file-list="false"
                                       :on-success="handleAvatarSuccess"
                                       :before-upload="beforeUpload"
                                       ref="upload">
                                <img v-if="imageUrl" :src="imageUrl" class="avatar">
                                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                            </el-upload>
                        </el-form-item>

                        <el-form-item>
                            <el-button type="primary" @click="AddNatatorium">确定添加</el-button>
                            <el-button @click="center">取消</el-button>
                        </el-form-item>
                    </el-form>
                </el-dialog>


            </div>
        </el-container>
    </el-container>
</div>


<script src="../../../plugins/vue/vue.js"></script>

<!-- 引入element-ui组件库 -->
<script src="../../../plugins/element-ui/index.js"></script>

<!-- 引入axios -->
<script src="../../../plugins/axios/axios.min.js"></script>

<script>

    new Vue({
        el:'#app',    //element
        data:{

            studentInfo:{}, //当前用户信息

            //搜索内容
            Query:{
                name:""
            },

            //显示的数据
            dataList:[],

            //element-ui分页插件
            pagination: {//分页相关模型数据 1
                currentPage: 1,//当前页码1
                pageSize:5,//每页显示的记录数1
                total:0,//总记录数1
            },

            //状态
            status: false,

            //图片组弹框内容
            dialog_image:false,
            imageList:'',



            //修改弹框内容
            formData_update:{},
            dialog_update:false,


            //新增弹框内容
            formData_add:{},
            dialog_add:false,

            options: [{
                typeName: '类型1',
                label: '类型1'
            }],

            value1: [new Date(), new Date()],
            value2: [new Date(), new Date()],

            //弹框标题
            title:'',

            // 存数据库图片组的字符串
            imageUrl:'',

            //图片组操作的图片列表
            fileList: [],
            disabled: false,  // 图片组弹框

            //图片组图片放大弹框的属性
            dialogImageUrl: '',
            dialogVisible: false,

        },

        created() {

            this.getAll();

        },
        methods: {

            getAll() {
                parent = "?name=" + this.Query.name;
                axios.get("/natatoriums/getPageNatatorium/" + this.pagination.currentPage + "/" + this.pagination.pageSize + parent).then((res) => {
                    console.log(res)
                    this.dataList = res.data.data.records
                    this.pagination.total = res.data.data.total
                    this.Query.name = ""
                })
            },

            //切换页码
            handleCurrentChange(currentPage) {
                console.log("切换页码:")
                this.pagination.currentPage = currentPage;
                console.log(currentPage)
                this.getAll();
            },

            //修改弹框的显示和隐藏
            Up_Add_dialogShow(row){

                //初始化options
                axios.get("/types/getAllType").then((res)=> {
                    this.options=res.data.data
                    console.log(this.options)
                })

                if (row!==null) {
                    this.title='修改'
                    //数据回显
                    this.formData_update = row
                    this.imageUrl = `/files/downloadFile?name=${row.image}`

                    //弹框显示
                    this.dialog_update=true
                }else {
                    this.title='新增'

                    this.imageUrl = ""
                    this.formData_add={}
                    this.dialog_add=true
                }
            },


            //确定添加
            AddNatatorium(){
                axios.post("/natatoriums/addNatatorium",this.formData_add).then((res)=> {
                    if (res.data.code){
                        this.$message.success(res.data.msg)
                    }else{
                        this.$message.error(res.data.msg)
                    }
                }).finally(()=>{
                    this.getAll();
                    this.dialog_add=false;
                })
            },

            //确定修改数据
            UpdateNatatorium(){
                console.log("确定修改：")
                console.log(this.imageList)
                this.formData_update.images=this.imageList

                axios.put("/natatoriums/updateNatatorium",this.formData_update).then((res)=>{     //修改数据回显
                    if (res.data.code){
                        this.$message.success(res.data.msg)
                    }else{
                        this.$message.error(res.data.msg)
                    }
                }).finally(()=>{
                    this.getAll();
                    this.dialog_update=false;
                    this.dialog_image=false

                })
            },


            //通过id删除
            DeleteNatatorium(row){
                this.$confirm("此操作删除当前泳池全部信息，是否继续?","提醒",{type: 'info'
                }).then(()=>{
                    axios.delete("/natatoriums/deleteNatatorium/"+row.id).then((res)=> {
                        console.log("deleteById:")
                        console.log(res)
                        if (res.data.code){
                            this.$message.success(res.data.msg)
                        }else{
                            this.$message.error(res.data.msg)
                        }
                    }).finally(()=>{
                        this.getAll()
                    })
                }).catch(()=>{
                    //取消删除
                    this.$message.info("取消删除操作")
                })
            },

            center() {
                this.dialog_update=false
                this.dialog_add=false
                this.dialog_image=false

                this.$message.info("取消操作")
                this.getAll()
            },

            //4.图片文件上传
            handleAvatarSuccess (response, file, fileList) {
                this.formData_add.image=response.data
                this.formData_update.image=response.data

                this.imageUrl= `/files/downloadFile?name=${response.data}`

                // 5985100
                console.log("测试：：：：")
                console.log(this.imageUrl)
                console.log("response:")
                console.log(response)
                console.log(file)
                console.log(fileList)
            },
            //图片组1：修改图片组弹框的显示和隐藏
            image_dialogShow(row){
                //这个操作是为了在后面确认修改的时候 只修改images（图片组属性）需要通过id修改做准备（不然不知道是修改哪条的图片组）
                this.formData_update.id=row.id

                this.title='修改图片组'

                //图片组图片回显(查看)
                //1.fileList每次进行空初始化
                this.fileList=[]
                //3.如果开始图片组没有数据就不进行字符串切割（不然会报错）=》让它直接打开弹框
                if (row.images!=null){
                    //2.对图片组进行字符串切割
                    var images = row.images.split(',')
                    //注：因为切割的字符串是’xxx,xxx,xxx,‘=>按逗号切割会将最后一个逗号切割成一个空字符串，所以for循环不用循环到最后一个元素
                    for (var i = 0; i < images.length-1; i++) {
                        this.fileList.push({name:images[i],url:'http://localhost:8080/files/downloadFile?name='+images[i]})
                    }
                    console.log(this.fileList)
                }
                this.dialog_image=true
            },
            //图片组3：图片组文件上传成功
            handleAvatarSuccess1 (response, file, fileList) {
                console.log(fileList[fileList.length-1].name)

                //将fileList最后上传的一张图片名字和url路径修改为上传成功之后的名字和对应服务器访问的路径
                fileList[fileList.length-1].name=response.data
                fileList[fileList.length-1].url='http://localhost:8080/files/downloadFile?name='+fileList[fileList.length-1].name


                this.fileList=fileList

                console.log(response)
                console.log(file)
                console.log(fileList)

                //拼接多图名称（存数据库图片组的字符串）
                this.imageList=''//初始化
                for (var i = 0; i < this.fileList.length; i++) {
                    this.imageList +=  this.fileList[i].name + ','
                }

                console.log(this.imageList)
            },
            //图片组4：图片组文件上传之后
            beforeUpload (file) {
                if(file){
                    const suffix = file.name.split('.')[1]
                    const size = file.size / 1024 / 1024 < 2
                    if(['png','jpeg','jpg'].indexOf(suffix) < 0){
                        this.$message.error('上传图片只支持 png、jpeg、jpg 格式！')
                        this.$refs.upload.clearFiles()
                        return false
                    }
                    if(!size){
                        this.$message.error('上传文件大小不能超过 2MB!')
                        return false
                    }
                    return file
                }
            },
            //图片组5：图片组图片(删除)
            handleRemove(file) {

                console.log(file);
                console.log(this.fileList);
                //通过循环数组找到要删除的那个图片的名字，然后从fileList中删除
                for (var i = 0; i < this.fileList.length; i++) {
                    if (this.fileList[i].name==file.name){
                        this.fileList.splice(i,1)  //从下标i开始删除一个（这里就是删除当前下标为i的数值）
                    }
                }

                //拼接多图名称（存数据库图片组的字符串）
                this.imageList=''//初始化
                for (var i = 0; i < this.fileList.length; i++) {
                    this.imageList +=  this.fileList[i].name + ','
                }
                console.log(this.fileList);
                console.log(this.imageList);
            },
            //图片组6：图片组图片放大
            handlePictureCardPreview(file) {
                this.dialogImageUrl = file.url;
                this.dialogVisible = true;
            },
            getImage (image) {
                this.imageUrl1= `/files/downloadFile?name=${image}`
            },

        }
    })

</script>
</body>
</html>




